package redis

import (
	"fleetmanager/logger"
	"fleetmanager/setting"
	"fmt"
	"github.com/go-redis/redis"
)

var TLogger *logger.FMLogger

func Init() error {
	RedisClient := GetRedisClient()
	rcd := RedisClient.Ping()
	if rcd.Val() != "PONG" {
		TLogger.Error("test ping redis error: %+v, addr:%s, password: *********, DB: %d",
			rcd, setting.RedisAddress, setting.RedisDB)
		return fmt.Errorf("test ping redis error: %+v", rcd)
	}
	return nil
}

func GetRedisClient() *redis.Client {
	RedisClient := redis.NewClient(&redis.Options{
		Addr:     setting.RedisAddress,
		Password: setting.RedisPassword,
		DB:       setting.RedisDB,
	})
	return RedisClient
}

func CloseRedisClient(client *redis.Client) {
	err := client.Close()
	if err != nil {
		TLogger.Error("[Redis] close client error for %v", err)
	}
}
